import os
from fastapi import APIRouter
import pandas as pd
template_app = APIRouter()

@template_app.get("/template")
async def template():
    try:
        df1 = pd.DataFrame({'国家代码': ['唯一值，必填'],
                            '国家名称': ['必填'],
                            '央库': ['选填'],
                            '保税': ['选填'],
                            '欧盟': ['选填'],
                            '元器件': ['选填']
                            })

        df2 = pd.DataFrame({'Item': ['必填'],
                            'CertificateCode': ['必填']
                            })

        df3 = pd.DataFrame({'Item': ['唯一值，必填'],
                            'ProductPlanner': ['必填']
                            })

        # 获取桌面路径
        desktop_path = os.path.join(os.environ['USERPROFILE'], 'Desktop', '模板.xlsx')
        with pd.ExcelWriter(desktop_path) as writer:
            df1.to_excel(writer, sheet_name='收件人', index=False)
            df2.to_excel(writer, sheet_name='运输报告', index=False)
            df3.to_excel(writer, sheet_name='产品计划', index=False)
        # 使用默认的程序打开文件
        os.startfile(desktop_path)
        return {
            "code": 200,
            "message": "下载成功"
        }
    except Exception as e:
        return {
            "code": 500,
            "message": f"下载失败: {str(e)}"
        }

